package ustc.xiaoyang.lingchashan.doublepointer;

import java.util.Arrays;

/**
 * 611.有效的三角形个数 medium
 */
public class TriangleNumber611 {
    public int triangleNumber(int[] nums){
        Arrays.sort(nums);
        int n = nums.length;
        int ans = 0;
        for (int i = n-1; i >= 0; i--) {
            int l = 0;
            int r = i - 1;
            while(l < r){
                int sum = nums[l] + nums[r] - nums[i];
                if(sum > 0){
                    ans += r - l;
                    r--;
                }else{
                    l++;
                }
            }
        }
        return ans;
    }
}
